package com.flot.yazb.aop.stub;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.flot.yazb.home.splash.SplashActivity;
import com.flot.yazb.widget.utils.ToastUtil;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public abstract class RMCallback<T extends RMResult> implements Callback<T> {

    public static final int R_OK = 200;
    public static final int R_Network_Error = 500;
    public static final int R_Unexpected_Error = 501;
    public static final int R_Unauthenticated_Error = 502;
    public static final int R_Client_Error = 503;
    public static final int R_Server_Error = 504;

    private Context mContext;
    private boolean mErrIgnore = false;
    String errorInfo = "";

    public RMCallback(Context context) {
        this.mContext = context;
    }

    public RMCallback(Context context, boolean errIgnore) {
        this.mErrIgnore = errIgnore;
        this.mContext = context;
    }

    protected abstract void onSuccess(T result);

    protected abstract void onFail(String errorCode, String errorInfo);

    protected void onFail(T content) {}

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (!isAlive()) return;
        int errorCode = response.code();
        if (errorCode == 200) {
            errorCode = R_OK;
        } else if (errorCode == 401) {
            errorCode = R_Unauthenticated_Error;
        } else if (errorCode >= 400 && errorCode < 500) {
            errorCode = R_Client_Error;
        } else if (errorCode >= 500 && errorCode < 600) {
            errorCode = R_Server_Error;
        } else {
            errorCode = R_Unexpected_Error;
        }

        T body = response.body();
        if (body != null) {
            errorInfo = body.message();
        } else {
            errorInfo = response.message();
        }

        if (null == body) {
            if (!mErrIgnore) onFail(errorCode + "", errorInfo);
            return;
        }

        if (body.verify() || body.changePassword()) {
            onSuccess(body);
            return;
        }

        if (body.finishGradua()) {
            intoEndGraduaActivity();
            onFail(body.code() + "", errorInfo);
            return;
        }
        if (body.startGradua()) {
            intoSatrtGraduaActivity();
            return;
        }
        if (body.failureToken()) {
            ReLogin();
            return;
        }

        if (body.isSetInFailed()) {
            onFail(body);
            onFail(body.code() + "", body.message());
        }

        if (R_OK != errorCode) {
            if (!mErrIgnore) onFail(body.code() + "", errorInfo);
            return;
        }
        if (!mErrIgnore) onFail(body.code() + "", body.message());
    }

    //跳转登录界面
    public void ReLogin() {
        ToastUtil.showToast(mContext, errorInfo);
        Intent intent = new Intent();
        intent.setClass(mContext, SplashActivity.class);
        mContext.startActivity(intent);
    }


    public void intoSatrtGraduaActivity() {
    }

    public void intoEndGraduaActivity() {
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {
        if (!isAlive()) return;
        if (!mErrIgnore) {
            if (t instanceof UnknownHostException) {
                onFail(R_Network_Error + "", "请求失败");
            } else if (t instanceof SocketTimeoutException) {
                onFail(R_Network_Error + "", "请求超时");
            } else if (t instanceof IOException) {
                onFail(R_Network_Error + "", t.getMessage());
            } else {
                onFail(R_Unexpected_Error + "", t.getMessage());
            }
        }
    }


    private boolean isAlive() {
        if (mContext == null)
            return false;
        if (mContext instanceof Activity) {
            if (((Activity) mContext).isFinishing())
                return false;
        }
        return true;
    }
}
